package service

import (
	"gin-vue-admin/global"
	"gin-vue-admin/model"
	"gin-vue-admin/model/request"
)

//@author: [piexlmax](https://github.com/piexlmax)
//@function: CreateConfig
//@description: 创建Config记录
//@param: config model.Config
//@return: err error

func CreateConfig(config model.Config) (err error) {
	err = global.GVA_DB.Create(&config).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: DeleteConfig
//@description: 删除Config记录
//@param: config model.Config
//@return: err error

func DeleteConfig(config model.Config) (err error) {
	err = global.GVA_DB.Delete(&config).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: DeleteConfigByIds
//@description: 批量删除Config记录
//@param: ids request.IdsReq
//@return: err error

func DeleteConfigByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]model.Config{},"id in ?",ids.Ids).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: UpdateConfig
//@description: 更新Config记录
//@param: config *model.Config
//@return: err error

func UpdateConfig(config model.Config) (err error) {
	err = global.GVA_DB.Save(&config).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetConfig
//@description: 根据id获取Config记录
//@param: id uint
//@return: err error, config model.Config

func GetConfig(id uint) (err error, config model.Config) {
	err = global.GVA_DB.Where("id = ?", id).First(&config).Error
	return
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetConfigInfoList
//@description: 分页获取Config记录
//@param: info request.ConfigSearch
//@return: err error, list interface{}, total int64

func GetConfigInfoList(info request.ConfigSearch) (err error, list interface{}, total int64) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&model.Config{})
    var configs []model.Config
    // 如果有条件搜索 下方会自动创建搜索语句
    if info.Key != "" {
        db = db.Where("`key` = ?",info.Key)
    }
    if info.Name != "" {
        db = db.Where("`name` LIKE ?","%"+ info.Name+"%")
    }
    if info.Type != 0 {
        db = db.Where("`type` = ?",info.Type)
    }
	if info.Value != `abcde` {
		db = db.Where("`key` != ? ",`p_close`)
	}
	err = db.Count(&total).Error
	err = db.Limit(limit).Offset(offset).Find(&configs).Error
	return err, configs, total
}